package com.comba.patch.tool;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.comba.patch.tool.utils.AlertUtils;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Bootstrap extends Application {
	private static final Logger LOGGER = LoggerFactory.getLogger(Bootstrap.class);
	private static Bootstrap instance = null;
	private Stage primaryStage;
	private BorderPane rootLayout;


	@Override
	public void start(Stage primaryStage) {
		instance = this;
		this.primaryStage = primaryStage;
		this.primaryStage.setTitle("补丁制作工具v1.0");
		this.primaryStage.getIcons().add(new Image(this.getClass().getResourceAsStream("/images/patch_32_32.png")));
		this.primaryStage.setResizable(false);
		this.primaryStage.centerOnScreen();

		this.initRootLayout();

		this.initPatchOverview();
	}

	public void initRootLayout() {
		try {
			FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/com/comba/patch/tool/view/RootLayout.fxml"));
			this.rootLayout = loader.load();
			Scene scene = new Scene(this.rootLayout);
			this.primaryStage.setScene(scene);
			this.primaryStage.show();
		} catch (IOException e) {
			String msg = "加载主界面失败";
			AlertUtils.error(msg);
			LOGGER.error(msg, e);
		}
	}

	public void initPatchOverview() {
		try {
			FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/com/comba/patch/tool/view/PatchOverview.fxml"));
			AnchorPane overviewPane = loader.load();
			this.rootLayout.setCenter(overviewPane);;
		} catch (IOException e) {
			String msg = "加载主界面失败";
			AlertUtils.error(msg);
			LOGGER.error(msg, e);
		}
	}


	public static void main(String[] args) {
		launch(args);
	}

	public static Bootstrap getInstance() {
		return instance;
	}

	public Stage getPrimaryStage() {
		return primaryStage;
	}
}
